//
//  PEAlarmListCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/27.
//

#import "RHAlarmFaultListCell.h"
@interface RHAlarmFaultListCell()

@property(nonatomic,strong)QMUILabel *alarmLevalTitleLab;
@property(nonatomic,strong)QMUILabel *alarmDeviceTitleLab;
@property(nonatomic,strong)QMUILabel *alarmDesTitleLab;

//告警码
@property(nonatomic,strong)QMUILabel *alarmCodeValueLab;
//等级
@property(nonatomic,strong)QMUILabel *alarmLevalValueLab;
//故障名称
@property(nonatomic,strong)QMUILabel *alarmDeviceValueLab;
//设备类型
@property(nonatomic,strong)QMUILabel *deviceTypeCodeLab;
//电站名称
@property(nonatomic,strong)QMUILabel *plantNameLab;
//描述
@property(nonatomic,strong)QMUILabel *alarmDesContentLab;
//上报时间
@property(nonatomic,strong)QMUILabel *reportDateLab;
//状态
@property(nonatomic,strong)QMUIButton *alarmStatusBtn;
@end
@implementation RHAlarmFaultListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}
-(void)setAlarmModel:(RHDeviceAlarmModel *)alarmModel
{
    _alarmModel = alarmModel;
    self.alarmCodeValueLab.text   = _alarmModel.faultCode;
    self.alarmLevalValueLab.text  = _alarmModel.faultLevel;
    if([_alarmModel.faultLevel integerValue] == 1){
        self.alarmLevalValueLab.text = @"告警".rh_localized;
        self.alarmLevalTitleLab.text  = [NSString stringWithFormat:@"%@:",@"告警等级".rh_localized];
        self.alarmDeviceTitleLab.text = [NSString stringWithFormat:@"%@:",@"告警设备".rh_localized];
        self.alarmDesTitleLab.text    = [NSString stringWithFormat:@"%@:",@"告警描述".rh_localized];
    }
    if([_alarmModel.faultLevel integerValue] == 2){
        self.alarmLevalValueLab.text = @"故障".rh_localized;
        self.alarmLevalTitleLab.text  = [NSString stringWithFormat:@"%@:",@"故障等级".rh_localized];
        self.alarmDeviceTitleLab.text = [NSString stringWithFormat:@"%@:",@"故障设备".rh_localized];
        self.alarmDesTitleLab.text    = [NSString stringWithFormat:@"%@:",@"故障描述".rh_localized];
    }
    if([_alarmModel.faultLevel integerValue] == 3){
        self.alarmLevalValueLab.text = @"保护".rh_localized;
        self.alarmLevalTitleLab.text  = [NSString stringWithFormat:@"%@:",@"保护等级".rh_localized];
        self.alarmDeviceTitleLab.text = [NSString stringWithFormat:@"%@:",@"保护设备".rh_localized];
        self.alarmDesTitleLab.text    = [NSString stringWithFormat:@"%@:",@"保护描述".rh_localized];
    }
    self.alarmDeviceValueLab.text = _alarmModel.deviceId;
    self.deviceTypeCodeLab.text = _alarmModel.deviceTypeCode;
    self.plantNameLab.text = ValidNullDataToString(_alarmModel.plantName, @"--");
    NSTimeInterval timestamp = _alarmModel.reportedTime/1000;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    self.reportDateLab.text = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timestamp]];
    self.alarmDesContentLab.text  = ValidNullDataToString(_alarmModel.faultDetail, @"--");
    NSString *lanKey = [NSString localLanguageMapToParamLanguage];
    NSDictionary *alarmDesDic = [alarmModel.faultDetail jsonStringToDictionary];
    if (alarmDesDic[lanKey]) {
        self.alarmDesContentLab.text = ValidNullData(alarmDesDic[lanKey]);
    }else{
        //没有找到对应的语言 ,显示英文
        self.alarmDesContentLab.text = alarmDesDic[@"en"]?alarmDesDic[@"en"]:alarmDesDic[@"zh"];
    }
    
    if (self.alarmDesContentLab.text.length == 0) {
        self.alarmDesContentLab.text = @"--";
        self.alarmDesContentLab.textAlignment = NSTextAlignmentRight;
    }
    [self.alarmDesContentLab sizeToFit];
    
    NSInteger row = self.alarmDesContentLab.size.height/self.alarmDesContentLab.font.lineHeight;
    if(row ==1){
        self.alarmDesContentLab.textAlignment = NSTextAlignmentRight;
    }else{
        self.alarmDesContentLab.textAlignment = NSTextAlignmentLeft;
    }
    
    if(alarmModel.audit == 0){
        [self.alarmStatusBtn setTitleColor:kCOLOR_HEX_A(@"#8B8D92", 1) forState:UIControlStateNormal];
        [self.alarmStatusBtn setTitle:@"已处理".rh_localized forState:UIControlStateNormal];
        self.alarmStatusBtn.layer.borderColor = kCOLOR_HEX_A(@"#A8ABB2",0.2).CGColor;
        [self.alarmStatusBtn setBackgroundColor:kCOLOR_HEX_A(@"#A8ABB2",0.15)];
        
    }else if (alarmModel.audit == 1){
        [self.alarmStatusBtn setTitleColor:kCOLOR_HEX(@"#F2A50F") forState:UIControlStateNormal];
        [self.alarmStatusBtn setTitle:@"未处理".rh_localized forState:UIControlStateNormal];
        self.alarmStatusBtn.layer.borderColor = kCOLOR_HEX_A(@"#F2A50F",0.2).CGColor;
        [self.alarmStatusBtn setBackgroundColor:kCOLOR_HEX_A(@"#F2A50F",0.15)];

    }else if (alarmModel.audit == 2){
        [self.alarmStatusBtn setTitleColor:kCOLOR_HEX(@"#009B11") forState:UIControlStateNormal];
        [self.alarmStatusBtn setTitle:@"处理中".rh_localized forState:UIControlStateNormal];
        self.alarmStatusBtn.layer.borderColor = kCOLOR_HEX_A(@"#009B11",0.2).CGColor;
        [self.alarmStatusBtn setBackgroundColor:kCOLOR_HEX_A(@"#009B11",0.15)];
        //终端用户以上可处理
        if([PrivateMange share].currentUser.hierarchyCode <= UserRoleTypeSmallInstaller){
            [self.alarmStatusBtn setTitle:@"处理".rh_localized forState:UIControlStateNormal];
        }
    }
    
    kWeakSelf;
    [self.alarmStatusBtn.titleLabel sizeToFit];
    [self.alarmStatusBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(weakSelf.alarmStatusBtn.titleLabel.width + 30);
    }];
    [_alarmDesTitleLab sizeToFit];
    [_alarmDesTitleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(weakSelf.alarmDesTitleLab.width+1);
    }];
}
-(void)setupUI{
    self.contentView.backgroundColor = kRGB(246, 248, 250);
    UIView *bgView = [[UIView alloc]init];
    [self.contentView addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    [bgView rounded:8 width:1 color:kBorderColor];
    //编号
//    QMUILabel *alarmCodeTitleLab = [[QMUILabel alloc]init];
//    alarmCodeTitleLab.font = [UIFont systemFontOfSize:14];
//    alarmCodeTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
//    alarmCodeTitleLab.textAlignment = NSTextAlignmentLeft;
//    alarmCodeTitleLab.text = @"告警码:".rh_localized;
//    [bgView addSubview:alarmCodeTitleLab];
    
    
    self.alarmCodeValueLab = [[QMUILabel alloc]init];
    self.alarmCodeValueLab.font = kBOLDSYSTEMFONT(16);
    self.alarmCodeValueLab.textColor = [UIColor colorWithHexString:@"3F4443"];
    self.alarmCodeValueLab.textAlignment = NSTextAlignmentLeft;
    self.alarmCodeValueLab.text = @"W1,W2";
    [bgView addSubview:self.alarmCodeValueLab];
    
    _alarmStatusBtn = [[QMUIButton alloc]init];
    _alarmStatusBtn.titleLabel.font = kSYSTEMFONT(12);
    [_alarmStatusBtn setTitleColor:kCOLOR_HEX_A(@"#3F444366", 0.4) forState:UIControlStateNormal];
    [_alarmStatusBtn setTitle:@"--" forState:UIControlStateNormal];
    _alarmStatusBtn.layer.cornerRadius = 8;
    _alarmStatusBtn.layer.borderWidth = 1;
    _alarmStatusBtn.layer.masksToBounds = YES;
    [bgView addSubview:_alarmStatusBtn];
    
    //等级
    _alarmLevalTitleLab = [[QMUILabel alloc]init];
    _alarmLevalTitleLab.font = [UIFont systemFontOfSize:12];
    _alarmLevalTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    _alarmLevalTitleLab.textAlignment = NSTextAlignmentLeft;
    _alarmLevalTitleLab.text = [NSString stringWithFormat:@"%@:",@"告警等级".rh_localized];
    [bgView addSubview:_alarmLevalTitleLab];
    
    self.alarmLevalValueLab = [[QMUILabel alloc]init];
    self.alarmLevalValueLab.font = [UIFont systemFontOfSize:12];
    self.alarmLevalValueLab.textColor = [UIColor colorWithHexString:@"3F4443"];
    self.alarmLevalValueLab.textAlignment = NSTextAlignmentRight;
    self.alarmLevalValueLab.text = @"--";
    [bgView addSubview:self.alarmLevalValueLab];
    
    //设备
    _alarmDeviceTitleLab = [[QMUILabel alloc]init];
    _alarmDeviceTitleLab.font = [UIFont systemFontOfSize:12];
    _alarmDeviceTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    _alarmDeviceTitleLab.textAlignment = NSTextAlignmentLeft;
    _alarmDeviceTitleLab.text = [NSString stringWithFormat:@"%@:",@"告警设备".rh_localized];
    [bgView addSubview:_alarmDeviceTitleLab];
    
    self.alarmDeviceValueLab = [[QMUILabel alloc]init];
    self.alarmDeviceValueLab.font = [UIFont systemFontOfSize:12];
    self.alarmDeviceValueLab.textColor = [UIColor colorWithHexString:@"3F4443"];
    self.alarmDeviceValueLab.textAlignment = NSTextAlignmentRight;
    self.alarmDeviceValueLab.text = @"--";
    [bgView addSubview:self.alarmDeviceValueLab];
    
    //设备类型
    QMUILabel *devtypeTitleLab = [[QMUILabel alloc]init];
    devtypeTitleLab.font = [UIFont systemFontOfSize:12];
    devtypeTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    devtypeTitleLab.textAlignment = NSTextAlignmentLeft;
    devtypeTitleLab.text = [NSString stringWithFormat:@"%@:",@"设备类型".rh_localized];
    [bgView addSubview:devtypeTitleLab];
    
    self.deviceTypeCodeLab = [[QMUILabel alloc]init];
    self.deviceTypeCodeLab.font = [UIFont systemFontOfSize:12];
    self.deviceTypeCodeLab.textColor = [UIColor colorWithHexString:@"3F4443"];
    self.deviceTypeCodeLab.textAlignment = NSTextAlignmentRight;
    self.deviceTypeCodeLab.text = @"--";
    [bgView addSubview:self.deviceTypeCodeLab];
    //电站名称
    QMUILabel *plantTitleLab = [[QMUILabel alloc]init];
    plantTitleLab.font = [UIFont systemFontOfSize:12];
    plantTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    plantTitleLab.textAlignment = NSTextAlignmentLeft;
    plantTitleLab.text = [NSString stringWithFormat:@"%@:",@"电站名称".rh_localized];
    [bgView addSubview:plantTitleLab];
    
    self.plantNameLab = [[QMUILabel alloc]init];
    self.plantNameLab.font = [UIFont systemFontOfSize:12];
    self.plantNameLab.textColor = [UIColor colorWithHexString:@"3F4443"];
    self.plantNameLab.textAlignment = NSTextAlignmentRight;
    self.plantNameLab.text = @"--";
    [bgView addSubview:self.plantNameLab];
    
    //描述
    _alarmDesTitleLab = [[QMUILabel alloc]init];
    _alarmDesTitleLab.font = [UIFont systemFontOfSize:12];
    _alarmDesTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    _alarmDesTitleLab.textAlignment = NSTextAlignmentLeft;
    _alarmDesTitleLab.text = [NSString stringWithFormat:@"%@:",@"告警描述".rh_localized];
    [bgView addSubview:_alarmDesTitleLab];
    
    self.alarmDesContentLab = [[QMUILabel alloc]init];
    self.alarmDesContentLab.font = [UIFont systemFontOfSize:12];
    self.alarmDesContentLab.textColor = [UIColor colorWithHexString:@"3F4443"];
    self.alarmDesContentLab.lineBreakMode = NSLineBreakByCharWrapping;
    self.alarmDesContentLab.numberOfLines = 0;
    self.alarmDesContentLab.textAlignment = NSTextAlignmentLeft;
    self.alarmDesContentLab.text = @"--";
    [bgView addSubview:self.alarmDesContentLab];
    
    //line
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor colorWithHexString:@"EBEDF0"];
    [bgView addSubview:line];
    
    
    //上报时间
    QMUILabel *reportTitleLab = [[QMUILabel alloc]init];
    reportTitleLab.font = [UIFont systemFontOfSize:12];
    reportTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    reportTitleLab.textAlignment = NSTextAlignmentLeft;
    reportTitleLab.text = [NSString stringWithFormat:@"%@:",@"上报时间".rh_localized];
    [bgView addSubview:reportTitleLab];
    
    self.reportDateLab = [[QMUILabel alloc]init];
    self.reportDateLab.font = [UIFont systemFontOfSize:12];
    self.reportDateLab.textColor = [UIColor colorWithHexString:@"3F4443"];
    self.reportDateLab.textAlignment = NSTextAlignmentRight;
    self.reportDateLab.text = @"2023-07-23 18:12:55";
    [bgView addSubview:self.reportDateLab];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 10, 5, 10));
    }];
    kWeakSelf;
//    [alarmCodeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(containView).offset(15);
//        make.top.equalTo(containView).offset(15);
//    }];
    [self.alarmCodeValueLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(15);
        make.top.equalTo(bgView).offset(15);
    }];
    
    [reportTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.alarmCodeValueLab);
        make.top.equalTo(weakSelf.alarmCodeValueLab.mas_bottom).offset(10);
    }];
    
    [self.reportDateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(reportTitleLab.mas_right).offset(5);
        make.centerY.equalTo(reportTitleLab);
    }];
    
    [self.alarmStatusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.height.mas_offset(30);
        make.top.mas_offset(15);
        make.width.offset(60);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.alarmCodeValueLab);
        make.top.equalTo(reportTitleLab.mas_bottom).offset(15);
        make.right.equalTo(weakSelf.alarmStatusBtn);
        make.height.offset(1);
    }];
    
    [_alarmLevalTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line);
        make.top.equalTo(line.mas_bottom).offset(15);
    }];
    [self.alarmLevalValueLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.alarmLevalTitleLab.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.alarmLevalTitleLab);
        make.right.equalTo(line);
    }];
   
    [_alarmDeviceTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line);
        make.top.equalTo(weakSelf.alarmLevalTitleLab.mas_bottom).offset(15);
    }];
    [self.alarmDeviceValueLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.alarmDeviceTitleLab.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.alarmDeviceTitleLab);
        make.right.equalTo(line);
    }];
    
    //设备类型
    [devtypeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line);
        make.top.equalTo(weakSelf.alarmDeviceTitleLab.mas_bottom).offset(15);
    }];
    [self.deviceTypeCodeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(devtypeTitleLab.mas_right).offset(5);
        make.centerY.equalTo(devtypeTitleLab);
        make.right.equalTo(line);
    }];
    //电站名称
    [plantTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line);
        make.top.equalTo(devtypeTitleLab.mas_bottom).offset(15);
    }];
    [self.plantNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(plantTitleLab.mas_right).offset(5);
        make.centerY.equalTo(plantTitleLab);
        make.right.equalTo(line);
    }];
    
    [_alarmDesTitleLab sizeToFit];
    
    [_alarmDesTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line);
        make.top.equalTo(plantTitleLab.mas_bottom).offset(15);
        make.width.offset(weakSelf.alarmDesTitleLab.width+1);
    }];
    [self.alarmDesContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.alarmDesTitleLab.mas_right).offset(5);
        make.top.equalTo(weakSelf.alarmDesTitleLab);
        make.right.equalTo(line);
        make.bottom.mas_offset(-20);
    }];
    
   
    [[_alarmStatusBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        //终端及以上用户才可处理
        if ([PrivateMange share].currentUser.hierarchyCode < UserRoleTypeTerminalUser) {
            if (weakSelf.alarmModel.audit == 1){
                if (weakSelf.confirmAction) {
                    weakSelf.confirmAction();
                }

            }
            if (weakSelf.alarmModel.audit == 2){
                if (weakSelf.processAction) {
                    weakSelf.processAction();
                }
            }
        }
       
    }];
    
}

@end
